<template>
  <div style="width:100%;" class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.idCard" placeholder="身份证号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('handle.search') }}</el-button>
        <el-button  type="primary" @click="addOrUpdateHandle()">{{ $t('handle.add') }}</el-button>
        <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">{{ $t('handle.batchDelete') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      ref="table"
      v-loading="dataListLoading"
      height="500"
      @selection-change="selectionChangeHandle"
      border
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        header-align="center"
        prop="name"
        align="center"
        :label="$t('form.name')">
      </el-table-column>
      <el-table-column
        prop="identificationNumber"
        header-align="center"
        align="center"
        label="身份证号">
      </el-table-column>
      <el-table-column
        prop="portraitId.photoUrl"
        header-align="center"
        align="center"
        label="图片">
         <template slot-scope="scope">
          <!-- <span style="color:red;">{{ scope.row.icon }}</span> -->
          <img :src="getPhoto(scope.row.portraitEntity)" style="width:50px;height:50px;" />
          <!-- {{ sexType(scope.row.portraitId) }} -->
          <!-- <img src="./momey.png" style="width:30%" /> -->
          <!-- <el-tag v-if="photoType === 1" size="small"><img :src="getPhoto(scope.row.portraitId)" style="width:50px;height:50px;border-radius:30px;" /></el-tag> -->
          <!-- <el-tag v-else-if="photoType === 2" size="small" type="info"><img :src="getPhoto(scope.row.portraitId)" style="width:50px;height:50px;border-radius:30px;" /></el-tag> -->
        </template>
      </el-table-column>
      <el-table-column
        prop="sex"
        header-align="center"
        align="center"
       :label="$t('form.sex')">
      </el-table-column>
      <el-table-column
        prop="nation"
        header-align="center"
        align="center"
        label="民族">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        :label="$t('handle.handle')" >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.populationCoding,true)">{{ $t('handle.detail') }}</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.populationCoding)">{{ $t('handle.update') }}</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.populationCoding)">{{ $t('handle.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10,20,50,100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top:5px;">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./add-or-update.vue";
import { getDictList, getPhotoUrl } from "@/utils";
import { imgUrlHost } from "@/config/env";
export default {
  data() {
    return {
      dataForm: {
        key: "",
        idCard: ""
      },
      photoType: 0,
      imgUrlHost: imgUrlHost,
      dataList: [],
      sexList: [],
      populationTypeList: [],
      typeDocumentList: [],
      populationCancellationTypeList: [],
      nationList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      imageurl: ""
    };
  },
  components: {
    AddOrUpdate
  },
  filters: {},
  computed: {
    getPhoto() {
      return function(type) {
        let photo = "";
        if (type === null) {
          photo = "../../../static/img/photo.png";
        } else {
          if (type.photoUrl !== null) {
            photo = this.imgUrlHost + type.photoUrl;
          } else if (type.photoBase64 !== null) {
            photo = "data:image/png;base64," + type.photoBase64;
          } else {
            photo = "../../../static/img/photo.png";
          }
        }
        return photo;
      };
    },
    regionId() {
      return this.$store.state.address.regionId;
    }
  },
  created() {
    this.initData();
  },
  methods: {
    adjust() {
      setTimeout(() => {
        this.$refs["table"].doLayout();
      }, 100);
    },
    initData() {
      getDictList("sex").then(({ data }) => {
        for (let i = 0; i < data.dictList.length; i++) {
          this.sexList[data.dictList[i].value] = data.dictList[i].label;
        }
      });
      getDictList("population_type").then(({ data }) => {
        for (let i = 0; i < data.dictList.length; i++) {
          this.populationTypeList[data.dictList[i].value] =
            data.dictList[i].label;
        }
      });
      getDictList("type_document").then(({ data }) => {
        for (let i = 0; i < data.dictList.length; i++) {
          this.typeDocumentList[data.dictList[i].value] =
            data.dictList[i].label;
        }
      });
      getDictList("nation").then(({ data }) => {
        for (let i = 0; i < data.dictList.length; i++) {
          this.nationList[data.dictList[i].value] = data.dictList[i].label;
        }
      });
      getDictList("population_cancellation_type").then(({ data }) => {
        for (let i = 0; i < data.dictList.length; i++) {
          this.populationCancellationTypeList[data.dictList[i].value] =
            data.dictList[i].label;
        }
        this.getDataList();
      });
    },
    // 获取数据列表
    getDataList() {
      setTimeout(() => {
        this.$refs["table"].doLayout();
      }, 100);
      let _this = this;
      this.dataListLoading = true;
      this.$http({
        url: "/dg-standard-info/dgregisterpopulation/list",
        method: "post",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          name: this.dataForm.key,
          idcard: this.dataForm.idCard,
          regionId: this.regionId
        })
      }).then(
        ({ data }) => {
          if (data && data.code === 0) {
            // this.dataList = data.page.list
            data.page.list.map(function(item) {
              // getPhotoUrl(item.portraitId).then(({data}) => {
              //   if(data.dgBasicPortrait){
              //     item.portraitId = data.dgBasicPortrait.photoUrl
              //   }else{
              //     item.portraitId = '../../../static/img/photo.png'
              //   }
              // })
              item.identificationType =
                _this.typeDocumentList[item.identificationType];
              item.sex = _this.sexList[item.sex];
              item.nation = _this.nationList[item.nation];
              item.managementCategory =
                _this.populationTypeList[item.managementCategory];
              item.logoutCategory =
                _this.populationCancellationTypeList[item.logoutCategory];
              return item;
            });
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount;
          } else {
            this.dataList = [];
            this.totalPage = 0;
            this.$message.error(data.msg);
          }
          this.dataListLoading = false;
        },
        error => {
          if (error.response.status === 403) {
            this.$message.error("你没有权限");
          } else {
            this.$message.error("系统错误，请联系管理员");
          }
        }
      );
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id, detail) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, detail);
        this.$refs.addOrUpdate.initPeople(id, detail);
      });
    },
    // getPhotoUrl (photoId) {
    //   this.$http({
    //     url: this.$http.adornUrl('/dgbasicportrait/info/'+photoId),
    //     method: 'get'
    //   }).then(({data}) => {
    //     console.log(data)
    //     if (data.dgBasicPortrait!== null) {
    //       this.imageurl = data.dgBasicPortrait.photoUrl
    //     } else {
    //       this.imageurl = ''
    //     }
    //   })
    // },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.populationCoding;
          });
      this.$confirm(`确定进行删除操作嘛?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: "/dg-ic/ic/personnel-basic/delete",
          method: "delete",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
<style>
#pane-four {
  overflow: auto;
}
</style>
